package cn.brian;

import java.util.concurrent.*;

public class Testsongsong {

    /**
     * newCachedThreadPool创建一个可缓存线程池，如果线程池长度超过处理需要，可灵活回收空闲线程，若无可回收，则新建线程。
     * newFixedThreadPool 创建一个定长线程池，可控制线程最大并发数，超出的线程会在队列中等待。
     * newScheduledThreadPool 创建一个定长线程池，支持定时及周期性任务执行。
     * newSingleThreadExecutor 创建一个单线程化的线程池，它只会用唯一的工作线程来执行任务，保证所有任务按照指定顺序(FIFO, LIFO, 优先级)执行。
     *
     *
     */
    /**
     * ThreadPoolExecutor.AbortPolicy:丢弃任务并抛出RejectedExecutionException异常。
     * ThreadPoolExecutor.DiscardPolicy：也是丢弃任务，但是不抛出异常。
     * ThreadPoolExecutor.DiscardOldestPolicy：丢弃队列最前面的任务，然后重新尝试执行任务（重复此过程）
     * ThreadPoolExecutor.CallerRunsPolicy：由调用线程处理该任务
     * @param args
     * @throws InterruptedException
     */
    public static void main(String[] args) throws InterruptedException {
        System.out.println(Runtime.getRuntime().availableProcessors());
        Testsongsong.testThreadPoolExecutor();

    }

    public static  void testThreadPoolExecutor() throws InterruptedException {
        ThreadPoolExecutor pool = new ThreadPoolExecutor(50, 50, 0, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(51), Executors.defaultThreadFactory(),new ThreadPoolExecutor.CallerRunsPolicy());
        for(int i=0;i<5;i++){
            System.out.println("" + i);
            CountDownLatch cdl = new CountDownLatch(50);
            for(int j=0;j<50;j++){
                pool.execute(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            TimeUnit.MILLISECONDS.sleep(5000);
                            //return 1;
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        } finally {
                            System.out.println(Thread.currentThread().getName());
                            cdl.countDown();
                        }
                    }
                });
    //                pool.submit(() -> {
    //                    try {
    //                        TimeUnit.MILLISECONDS.sleep(5000);
    //                        return 1;
    //                    } finally {
    //                        System.out.println(Thread.currentThread().getName());
    //                        cdl.countDown();
    //                    }
    //                });
            }
            cdl.await();
            System.out.println("finished........");
    }
    pool.shutdown();
}

    public static void testNewFixedThreadPool() throws InterruptedException {
        ExecutorService threadPool = Executors.newFixedThreadPool(50);
        for(int i=0;i<5;i++){
            System.out.println("" + i);
            CountDownLatch cdl = new CountDownLatch(50);
            for(int j=0;j<50;j++){
                threadPool.execute(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            TimeUnit.MILLISECONDS.sleep(5000);
                            //return 1;
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        } finally {
                            System.out.println(Thread.currentThread().getName());
                            cdl.countDown();
                        }
                    }
                });
//                pool.submit(() -> {
//                    try {
//                        TimeUnit.MILLISECONDS.sleep(5000);
//                        return 1;
//                    } finally {
//                        System.out.println(Thread.currentThread().getName());
//                        cdl.countDown();
//                    }
//                });
            }
            cdl.await();
            System.out.println("finished........");
        }
        threadPool.shutdown();
    }

}

